# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
from time import sleep
import datetime
class FruitnamerPipeline:
    conn =None
    cursor = None
    dateTamp=None
    host = "192.168.0.113"
    port = 3306
    user='coname'
    password = 'xZS6p4LDcKCwn4Yb'
    db="co"
    charset="utf8"

    
    def open_spider(self,spider):
        try:
            self.conn = pymysql.Connection(host=self.host,port=self.port,user=self.user,password=self.password,db=self.db,charset=self.charset)
            print("连接数据成功！")
        except Exception as e:
            print("连接数据失败!>>>",e)
        self.dateTanp='{0:%m%y%d%H%M%S}'.format(datetime.datetime.now())
        self.dateTamp=int(self.dateTanp) 
        self.n=1
    def process_item(self, item, spider):
        self.cursor= self.conn.cursor()
        sleep(1)
        self.n=self.n+1
        lis={'西瓜':52106164948,'红毛丹':52106164949,'蓝莓':52106164950,'水果':52106164951,'荔枝':52106164952,'李子':52106164953,'苹果':52106164954,'杨桃':52106164955,'葡萄':52106164956,'桃子':52106164957,'火龙果':52106164958,'香蕉':52106164959,'无花果':52106164960,'仙人掌果':52106164961,'橘子':52106164962,'葡萄柚':52106164963,'柠檬':52106164964,'石榴':52106164965,'桑葚':52106164966,'梨子':52106164967,'草莓':52106164968,'樱桃':52106164969,'黑莓':52106164970,'菠萝蜜':52106164971,'青啤梨':52106164972,'椰子':52106164973,'杨桃':52106164973,'猕猴桃':52106164974,'芒果':52106164975,'枣':52106164976,'柚':52106164977,'冬枣':52106164978,'柿子':52106164979,'梨':52106164980,'桃':52106164981,'龙眼':52106164982}
        try:
            # if not self.cursor.execute('select chapte from co_books_look_contents where chapte="{}"'.format(item["art_chapte"])):
            
            # if not self.cursor.execute('select srname from co_fruit_detail where chapte="{}"'.format(item["art_chapte"])):
 
            #     sql = 'insert into co_fruit_detail (content,srname,pid,srtime,chapte,srfrom,srauthor) values("{}","{}","{}","{}","{}","{}","{}")'.format(item["art_content"],item["art_name"],item['art_pid'],item['art_time'],item['art_chapte'],item['art_from'],item['art_author'])
            #     self.cursor.execute(sql)
            #     self.conn.commit()
            #     print('插入成功!')
            #     return item
            # sql = 'insert into co_books_look_contents(auther) value("{}") where pid="{}"'.format(item["art_pid"],item["art_author"])
            # sql='update  co_books_look_content set author="{}" where chapte="{}"'.format(item["art_author"],item["art_chapte"])
            sql="update  co_fruit set content='{}' where srname='{}'".format(item['art_content'],item["art_name"])
            # # if self.cursor.execute('select chapte from co_books_look_content where chapte ="{}"'.format(item['art_chapte']))!="[]":
            self.cursor.execute(sql)
            print("插入内容成功！")
            # print(item)
            self.conn.commit()
            return item
         
            # else:
            #     self.cursor.execute('update  co_books_look_content set author="{}" where pid="{}"'.format(item["art_author"],item["art_pid"]))
            #     print("插入内容成功！")
            #     print(item)
            #     self.conn.commit()
            #     return item
        except Exception as e:
            print("插入数据错误>>>",e)
            self.conn.rollback()
        # self.cursor.execute("update  co_books_look set fileurl='{}' where srname='{}'".format(item["art_url"],item["art_name"]))
       

    def close_spider(self,spider):
        try:
            self.conn.cursor().close()
            sleep(1)
            self.conn.close()
            print("关闭成功！")
        except Exception as e:
            print("关闭失败>>>",e)